package com.dongdong.service.impl;

import com.dongdong.mapper.TbItemMapper;
import com.dongdong.model.TbItem;
import com.dongdong.model.TbItemExample;
import com.dongdong.service.ItemService;
import com.dongdong.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper tbItemMapper;


    @Override
    public List<ItemVO> findItemByGoodId(Long goodId) {

            TbItemExample example = new TbItemExample();
            example.createCriteria().andGoodsIdEqualTo(goodId);
            List<TbItem> tbItems = tbItemMapper.selectByExample(example);//转换
        List<ItemVO> itemVOS = tbItems.stream().map((x) -> {
            ItemVO itemVO = new ItemVO();
            BeanUtils.copyProperties(x, itemVO);
            return itemVO;
        }).collect(Collectors.toList());
        return itemVOS;
    }

    @Override
    public TbItem selectBySkuId(Long skuId) {
        return tbItemMapper.selectByPrimaryKey(skuId);
    }

    @Override
    public void updateStockCount(TbItem tbItem) {
        tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }


}
